#ifndef _INTEGRAL_
#define _INTEGRAL_

#include <math.h>

#include "Density.h"

#define PI 3.141

double integral(double* phi) {
    double s = 0;
    for (int i = 0; i < N; ++i) {
        double iN = double(i) / N;
        for (int j = 0; j < N; ++j) {
            double jN = double(j) / N;
            if ((0.5 - iN) * (0.5 - iN) + (0.5 - jN) * (0.5 - jN) < 0.25) {
                s += phi[i * N + j];
            }
        }
    }
    return 4 * s / (PI * N * N);
}

double integral2(double* ps, double* phi) {
    double s = 0;
    for (int i = 0; i < N; ++i) {
        double iN = double(i) / N;
        for (int j = 0; j < N; ++j) {
            double jN = double(j) / N;
            double min = 2;
            for (int k = 0; k < K; k += 2) {
                double d = (ps[k] - iN) * (ps[k] - iN) + (ps[k + 1] - jN) * (ps[k + 1] - jN);
                if (d < min) {
                    min = d;
                }
            }
            if ((0.5 - iN) * (0.5 - iN) + (0.5 - jN) * (0.5 - jN) < 0.25) {
                s += sqrt(min) * phi[i * N + j];
            }
        }
    }
    return 4 * s / (PI * N * N);
}

#endif
